//类似全局路由守卫
import React, { useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
interface Props { }
function RouterBeforeEach(props: Props) {
  
  //从store中获取token
  var token = localStorage.getItem("token")
  //用来获取路由信息的钩子函数useLocation();
  var location = useLocation();
  //获取跳转路由方法
  var navigate = useNavigate();
  useEffect(() => {
    if (!token) {//没有token,跳到登录页
      navigate('/login');
      if(location.pathname == '/register'){
        navigate('/register');
      }
    } else if (location.pathname == '/login') {
      navigate('/index');
    }
  }, [location.pathname])//监听路由地址
  
  return (
    <></>
  );
}


export default RouterBeforeEach;